/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.util.List;
import model.Elev;
import model.Hold;
import model.HoldFag;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * ttodddj kda
 *
 * @author Marc
 */
public class HoldHandler {

    private Session session;

    public HoldHandler(Session session) {
        this.session = session;
    }

    public Hold[] getHold() throws HibernateException {

        String hql = "from model.Hold";
        session.beginTransaction();
        Query q = session.createQuery(hql);
        List resultList = q.list();
        Hold[] hold = new Hold[resultList.size()];
        for (int i = 0; i < resultList.size(); i++) {

            hold[i] = (Hold) resultList.get(i);
        }
        session.getTransaction().commit();
        return hold;
    }

    public Elev[] getElev(Hold hold) {

        Object[] obArr = hold.getElevs().toArray();
        Elev[] elArr = new Elev[obArr.length];
        for (int i = 0; i < obArr.length; i++) {
            elArr[i] = (Elev) obArr[i];
        }
        return elArr;
    }

    public HoldFag[] getHoldFag(Hold hold) {

        Object[] obArr = hold.getHoldFags().toArray();
        HoldFag[] elArr = new HoldFag[obArr.length];
        for (int i = 0; i < obArr.length; i++) {
            elArr[i] = (HoldFag) obArr[i];
        }
        return elArr;
    }

    public void sletHold(Hold hold) throws HibernateException {
        session.beginTransaction();
        String hql = "from model.Hold";
        session.createQuery(hql);
        session.flush();
        session.delete(hold);
        session.getTransaction().commit();
    }

    public void nytHold(Hold hold) {

        session.beginTransaction();
        String hql = "from Hold";
        session.flush();
        session.createQuery(hql);
        session.save(hold);
        session.getTransaction().commit();
    }

    public void redigerHold(Hold nyt, Hold old) {
        nytHold(nyt);
        session.beginTransaction();
        String hql = "from Hold";
        Elev[] elever = getElev(old);
        for (int i = 0; i < elever.length; i++) {
            elever[i].setHold(nyt);
            nyt.getElevs().add(elever[i]);
        }

        HoldFag[] holdFag = getHoldFag(old);
        for (int i = 0; i < holdFag.length; i++) {
            holdFag[i].setHold(nyt);
            holdFag[i].getId().setHold(nyt.getHoldId());
            nyt.getHoldFags().add(holdFag[i]);
            
        }
old.getElevs().clear();
old.getHoldFags().clear();
        session.createQuery(hql);
        session.delete(old);
        session.flush();
        session.getTransaction().commit();
    }
    
    public void sletFag(HoldFag holdFaget) throws HibernateException {
            
        session.beginTransaction();
        String hql = "from HoldFag";
        session.flush();
        session.createQuery(hql);
        holdFaget.getFag().getHoldFags().remove(holdFaget);
        holdFaget.getHold().getHoldFags().remove(holdFaget);
        session.delete(holdFaget);
        session.getTransaction().commit();
    }

    public void opretHoldFag(HoldFag holdfag) throws HibernateException {
        session.beginTransaction();
        String hql = "from HoldFag";
        session.createQuery(hql);
        session.flush();
        session.save(holdfag);
        holdfag.getHold().getHoldFags().add(holdfag);
        holdfag.getFag().getHoldFags().add(holdfag);
        session.getTransaction().commit();
        updateHold(holdfag.getHold());
    }

    private void updateHold(Hold hold) {

        session.beginTransaction();
        String hql = "from Hold";
        session.createQuery(hql);
        session.flush();
        session.update(hold);
        session.getTransaction().commit();

    }
}
